home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_9 / a9_1.m next >
Encoding:
Text File  |  1994-06-05  |  1.6 KB  |  71 lines  |  [MATS/MATL]

  1. echo off;
  2. % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
  3. % To accompany the text:
  4. % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
  5. % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
  6. % This free software is complements of the author.
  7.  
  8. % Algorithm 9.1 (Euler's Method).
  9. % Section    9.2,    Euler's Method, Page 435
  10. echo on; clc; format long; hold off; clear
  11.  
  12. % This program implements Euler's method
  13.  
  14. % for solving the initial value problem
  15.  
  16. %     y' = f(t,y)    with    y(a) = y0
  17.  
  18. %    Define and store the function f(t,y)    in the M-file  f.m 
  19. % function z = f(t,y)
  20. % z = (t-y)/2;
  21.  
  22. delete f.m
  23. diary f.m; disp('function z = f(t,y)');...
  24.            disp('z = (t-y)/2;');...
  25. diary off;
  26.  
  27. f(0,0); % Remark. f.m and euler.m are used for Algorithm 9.1
  28. pause      % Press any key to continue.
  29.  
  30. clc;
  31. %    Place the endpoints of [a,b] in  a  and  b.
  32.  
  33. %    Place the initial value y(a) in  ya.
  34.  
  35. %    Place the number of subintervals in  m.
  36.  
  37. a  = 0;
  38. b  = 3;
  39. ya = 1;
  40. m  = 12;
  41.  
  42. [T,Y] = euler('f',a,b,ya,m);
  43. points = [T;Y];
  44.  
  45. pause    % Press any key to see the list of solution points.
  46.  
  47. clc;, clg;
  48. c = 0;
  49. d = 1.75;
  50. axis([a b c d]);...
  51. plot(T,Y,'g');...
  52. hold on;...
  53. plot([a b],[0 0],'b',[0 0],[c d],'b');...
  54. if m<=30,
  55.   plot(T,Y,'or');
  56. end;...
  57. xlabel('t');...
  58. ylabel('y');...
  59. title('Euler`s solution to y` = f(t,y)');...
  60. grid;...
  61. axis;...
  62. hold off;...
  63. shg; pause    % Press any key to continue.
  64.  
  65. Mx1 = 'Euler`s solution to y` = f(t,y).';
  66. Mx2 = '     t(k)               y(k)';
  67. clc,echo off,diary output,...
  68. disp(''),disp(Mx1),...
  69. disp(''),disp(Mx2),disp(points'),diary off,echo on
  70.  
  71.